<?php

/**
 * 
 * @author Nguyen Van Nhieu <nhieunv@evolable.asia>
 * Mar 25, 2013
 * File: target_of_watch.php
 *
 **/ 
class TargetOfWatch extends AppModel {
	var $useTable = 'target_of_watch';
	
	/**
	 * check exist record with $userId && $teacherId
	 * @param int $userId
	 * @param int $teacherId
	 * @return boolean
	 */
	function isExistsRecord($userId, $teacherId) {
		$total = $this->find('count', array('conditions' => array(
			$this->alias.'.user_id' => $userId,
			$this->alias.'.watch_id' => $teacherId
		)));
		if($total > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 
	 * @param array $data
	 * @return boolean
	 */
	function saveData($data) {
		if(is_array($data)){
			try{
				return $this->save($data);
			}catch(Exception $ex){
				return false;
			}
		}
	}
	
	/**
	 * 
	 * @param int $target_watch_id
	 * @return boolean
	 */
	function deleteData($target_watch_id, $userId) {
		return $this->deleteAll(array(
			'id' => $target_watch_id,
			'user_id' => $userId
		));
	}
	
	function getTargetOfWatchInformationByUserIdAndTeacherId($userId, $teacherId) {
		return $this->find('first', array(
			'conditions' => array(
				$this->alias.'.user_id' => $userId,
				$this->alias.'.watch_id' => $teacherId
			)
		));
	}
	
	function getTargetOfWatchEmailListByTeacherId($teacherId) {
		$options['fields'] = array('User.email', 'User.id', 'Profile.first_name', 'Profile.last_name');
		$options['joins'] = array(
			array(
				'table' => 'users',
				'alias' => 'User',
				'type' => 'inner',
				'conditions' => array(
					$this->alias.'.user_id = User.id',
					'User.del_flg = 0',
					$this->alias.'.watch_id' => $teacherId
				),
			),
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					'User.id = Profile.user_id'
				)
			)
		);
		$options['recursive'] = 1;
		$options['conditions'] = array(
			$this->alias.'.watch_id' => $teacherId
		);
		return $this->find('all', $options);
	}
}